package com.project.erp.service.fobcost.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.project.erp.common.utils.Page;
import com.project.erp.dao.fobcost.FobCostDao;
import com.project.erp.dao.fobcost.FobCostDetailDao;
import com.project.erp.model.fobcost.FobCost;
import com.project.erp.model.fobcost.FobCostDetail;
import com.project.erp.service.fobcost.FobCostService;

@Service
@Transactional
public class FobCostServiceImpl implements FobCostService {

	@Autowired
	private FobCostDao fobCostDao;
	@Autowired
	private FobCostDetailDao fobCostDetailDao;
	
	@Override
	public Page<FobCost> getFobCostPageBySearch(Page<FobCost> page,
			FobCost fobCost) {
		return fobCostDao.getFobCostPageBySearch(page, fobCost);
	}

	//出现Exception异常也回滚事务
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void saveFobCost(FobCost fobCost) {
		if(fobCost.getCreateTime() == null){
			fobCost.setCreateTime(new Date());
		}
		fobCostDao.save(fobCost);
	}

	//只读事务，可以提高效率
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public FobCost getFobCostById(Long fobCostId) {
		
		return fobCostDao.get(fobCostId);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void editFobCost(FobCost fobCost,
			List<FobCostDetail> delFobCostDetailList, Long[] fobCostDetailIds) {
		List<Long> delFobCostDetailIds = new ArrayList<Long>();
		boolean delflag;//是否删除记录
		
		//检查是否有删除的记录
		if(fobCostDetailIds != null){//针对细表记录为空情况
			for(FobCostDetail fobdtl:delFobCostDetailList){
				delflag = true;
				String fobCostDetailId1 = "";
				String fobCostDetailId2 = "";
				for(int i=0;i<fobCostDetailIds.length;i++){
					
					if(fobCostDetailIds[i] != null){
						fobCostDetailId1 = String.valueOf(fobCostDetailIds[i]);
						fobCostDetailId2 = String.valueOf(fobdtl.getId());
						if(fobCostDetailId1.equals(fobCostDetailId2)){
							delflag = false;
						}
					}
				}
				if(delflag){
					//增加要删除的记录id到列表
					delFobCostDetailIds.add(fobdtl.getId());
				}
			}
		}else{
			//删除全部细表记录
			if((delFobCostDetailList != null) && (delFobCostDetailList.size() > 0)){
				for(FobCostDetail fob:delFobCostDetailList){
					delFobCostDetailIds.add(fob.getId());
				}
			}
		}
		
		//先删除要删除的细表记录
		if((delFobCostDetailIds != null) && (delFobCostDetailIds.size() > 0)){
			fobCostDetailDao.deleteByIdsList(delFobCostDetailIds);
		}
		
		fobCost.setUpdateTime(new Date());
		fobCostDao.save(fobCost);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void delFobCost(String[] fobCostIds) {
		if(null == fobCostIds || fobCostIds.length == 0){
			return;
		}
		List<Long> ids = new ArrayList<Long>();
		for (String fobCostId : fobCostIds) {
			ids.add(Long.valueOf(fobCostId));
		}
		//删除主表信息
		fobCostDao.delFobCostByfobCostIds(ids);
	}

	//只读事务，可以提高效率
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public FobCost getFobCostByItemAndSize(String item, String fobSize) {
		return fobCostDao.getFobCostByItemAndSize(item,fobSize);
	}
}
